from typing import Annotated, Any
from pydantic import AfterValidator, PlainSerializer, WithJsonSchema

# 实现自定义一个注册密码类型
Password = Annotated[
    str,
    AfterValidator(lambda x: valid_password(x)),
    PlainSerializer(lambda x: x, return_type=str),
    WithJsonSchema({'type': 'string'}, mode='serialization')
]

def valid_password(value: str) -> str:
    """
    Password requirements for registration: minimum 8 characters, including both numbers and letters.
    :param value: password
    :return: password
    """
    if not value or len(value) < 8:
        raise ValueError("please input minimum 8 characters")

    has_digit = False
    has_alpha = False
    for char in value:
        if char.isdigit():
            has_digit = True
        elif char.isalpha():
            has_alpha = True

    if not has_digit or not has_alpha:
        raise ValueError("please input both numbers and letters")
    return value
